package com.work.service.impl;

import com.work.bean.Student;
import com.work.bean.Teacher;
import com.work.dao.DBDao;
import com.work.dao.impl.DBImpl;
import com.work.entities.MyClass;
import com.work.service.UserService;
import com.work.utils.Constants;
import com.work.utils.SQLUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/9/29.
 */
public class UserServiceImpl implements UserService {

    DBDao dao;

    public UserServiceImpl() {
        dao = new DBImpl();
    }

    @Override
    public boolean verifyStuName(String name) {

        return dao.find(SQLUtil.findByStuUsernameSql(name), Constants.STUDENT).size() > 0 ? true : false;
    }

    @Override
    public boolean verifyTeacherName(String name) {
        return dao.find(SQLUtil.findByTeacherUsernameSql(name),Constants.TEACHER).size() > 0 ? true : false;
    }

    @Override
    public boolean insertStudent(Student student) {
        return dao.insertAndDelAndUpdate(SQLUtil.insertStudentSql(student));
    }

    @Override
    public boolean insertTeacher(Teacher teacher) {
        return dao.insertAndDelAndUpdate(SQLUtil.insertTeacherSql(teacher));
    }

    @Override
    public Student loginStudent(Student student) {

        List students = dao.find(SQLUtil.loginStudent(student), Constants.STUDENT);
        if (students.size() > 0){
            return (Student) students.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Teacher loginTeacher(Teacher te) {

        List teachers = dao.find(SQLUtil.loginTeacher(te),Constants.TEACHER);
        if (teachers.size() > 0){
            return (Teacher) teachers.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<MyClass> stuGetMyClass(int studentId) {
        return dao.find(SQLUtil.findMyClassByStuId(studentId),Constants.MYCLASS);
    }

    @Override
    public boolean updateStudentClassId(Student student) {
        return dao.insertAndDelAndUpdate(SQLUtil.updateStudent(student));
    }

}
